package com.lei.substringmatch;

/**
 * @Author LeiXiaoLei
 * @Date 2022/9/3 10:13
 * @Version 1.0
 */
public class SubstringMatch {
    private SubstringMatch() {
    }

    public static int bruteforce(String s, String t) {
        if (s.length() < t.length()) return -1;
        for (int i = 0; i + t.length() - 1 < s.length(); i++) {
            int j = 0;
            for (; j < t.length(); j++) {
                if (s.charAt(i + j) != t.charAt(j)) break;
            }
            if (j == t.length()) return i;
        }
        return -1;
    }

    public static void main(String[] args) {
        String s1 = "hello ,this is lei xiao lei";
        String t1 = "lei";
        System.out.println(SubstringMatch.bruteforce(s1, t1));
        SubstringMatchHelper.matchTest("bruteforce", s1, t1);
        String s2 = FileOperation.readFile("pride-and-prejudice.txt");
        String t2 = "china";
        SubstringMatchHelper.matchTest("bruteforce", s2, t2);

        int n = 10000000,m=1000;
        StringBuilder s3 = new StringBuilder();
        for (int i = 0; i < n; i++) {
            s3.append('a');
        }
        StringBuilder t3 = new StringBuilder();
        for (int i = 0; i < m-1; i++) {
            t3.append('a');
        }
        t3.append('b');
        SubstringMatchHelper.matchTest("bruteforce", s3.toString(), t3.toString());
    }
}
